package com.yf.cmw.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.yf.cmw.constant.BussConstant;
import com.yf.cmw.presenter.MainPresenter;

/**
 * U盘监控
 * @author chengmingwei
 * @date 2020-06-18 220:44
 */
public class USBdiskReceiver extends BroadcastReceiver {
    private static final String TAG = "USBdiskReceiver";

    private static MainPresenter mainPresenter;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String path = intent.getData().getPath();
        Log.i(TAG, String.format("action : %s , path : %s", action, path));
        if (action.equals(BussConstant.ACTION_USB_DEVICE_ATTACHED)) {
             Log.e("action===", "装载");

        } else if (action.equals(BussConstant.ACTION_USB_DEVICE_DETACHED)) {
            Log.d("action===", "卸载USB");
        }  else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
            Log.d(TAG, "ACTION_MEDIA_UNMOUNTED");
            // processUnmountedMessage(context, bundle);
        } else if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
            Log.d(TAG, "ACTION_MEDIA_MOUNTED");
           // processMountedMessage(context, path);
            copyFiles(context, path);
        } else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {
            Log.d(TAG, "ACTION_MEDIA_REMOVED");
            // processUnmountedMessage(context, bundle);
        }
    }

    private void copyFiles(Context context, String path){
        MainPresenter mainPresenter = new MainPresenter(context);
        mainPresenter.copyUsbADSToMachine(path);
    }
}
